package com.winning.pmph.ctrl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.winning.pmph.entity.IncreaseCategory;
import com.winning.pmph.service.IncreaseCategoryService;
import com.winning.pmph.utils.PMPHAppUtil;
import com.winning.pmph.vo.CategorySelectVo;
import com.winning.pmph.vo.CategoryVo;
import com.winning.pmph.vo.IncreaseCategoryVo;
import com.winning.pmph.vo.IncreaseResourceVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lbm
 * @since 2021-12-30
 */
@Api(value = "图书增值服务2")
@RestController
@RequestMapping("/pmph/action/increaseCategory")
public class IncreaseCategoryCtrl {
	@Autowired
    private IncreaseCategoryService increaseCategoryService;

	@ApiOperation(value = "selelctPageList", notes = "获取增值服务分类列表接口")
    @GetMapping("selelctPageList")
    public Page<IncreaseCategory> selelctPageList(String name, Integer currentPage, Integer pageSize) {
        if (currentPage == null || currentPage == 0) {
            currentPage = 1;
        }
        if (pageSize == null || pageSize == 0) {
            pageSize = 10;
        }
        Page<IncreaseCategory> page = increaseCategoryService.lambdaQuery()
                .eq(StringUtils.isNotBlank(name), IncreaseCategory::getName, name)
                .page(new Page<>(currentPage, pageSize));
        return page;
    }

	@ApiOperation(value = "selectCategoryTree", notes = "获取增值服务分类树")
    @GetMapping("selectCategoryTree")
    public List<CategoryVo> selectCategoryTree(String name, String bookId) {
        if (StringUtils.isBlank(bookId)) {
            throw new RuntimeException("图书id为空");
        }
        return increaseCategoryService.getCategoryTree(bookId);
    }
	
	@ApiOperation(value = "selectCategorySelectTree", notes = "获取增值服务分类选择树")
	@GetMapping("selectCategorySelectTree")
	public List<CategorySelectVo> selectCategorySelectTree(String name, String bookId) {
		if (StringUtils.isBlank(bookId)) {
			throw new RuntimeException("图书id为空");
		}
		List<CategorySelectVo> list = increaseCategoryService.getCategorySelectTree(bookId);
		return list;
	}
	
	@ApiOperation(value = "uploadFile", notes = "导入分类接口")
	@RequestMapping("uploadFile")
    public Map<String, Object> uploadFile(@RequestParam("file") MultipartFile file,@RequestParam("bookId") String bookId) throws Exception {
        Map<String, Object> result = new HashMap<>();
        increaseCategoryService.importCategory(file, bookId);
        return result;
    }

	@ApiOperation(value = "add", notes = "添加分类接口")
    @RequestMapping("add")
    public void add(@RequestBody IncreaseCategory increaseCategory) {
        increaseCategory.setCreateTime(new Date());
        increaseCategory.setUpdateTime(new Date());
        increaseCategory.setCreateBy(PMPHAppUtil.getCurrentUserEnName());
        increaseCategory.setUpdateBy(PMPHAppUtil.getCurrentUserEnName());
        increaseCategoryService.save(increaseCategory);
    }

	@ApiOperation(value = "update", notes = "更新分类接口")
    @RequestMapping("update")
    public void update(@RequestBody IncreaseCategory increaseCategory) {
        increaseCategory.setUpdateTime(new Date());
        increaseCategory.setUpdateBy(PMPHAppUtil.getCurrentUserEnName());
        increaseCategoryService.updateById(increaseCategory);
    }

	@ApiOperation(value = "selectById", notes = "获取分类接口")
    @RequestMapping("selectById")
    public IncreaseCategory selectById(String id) {
        return increaseCategoryService.getById(id);
    }

	@ApiOperation(value = "delete", notes = "删除分类接口")
    @RequestMapping("delete")
    public void delete(String id) {
        increaseCategoryService.removeById(id);
        String userName = PMPHAppUtil.getCurrentUserEnName();
        increaseCategoryService.sendDeleteCateData(id, userName);
    }
	
	/**
	 * 上下移动目录
	 * @param increaseResourceVo
	 * @return
	 * @throws Exception
	 */
	@ApiOperation(value = "upOrDownCate", notes = "上下移动资源分类")
	@RequestMapping("upOrDownCate")
    public void upOrDownCate(@RequestBody IncreaseCategoryVo increaseCategoryVo) throws Exception {
		increaseCategoryService.upOrDownCate(increaseCategoryVo.getId(),increaseCategoryVo.getUpDownFlag());
    }
}

